package com.ctgu.elasticsearch.utils;

import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Optional;

@Data
@Builder
@NoArgsConstructor
public class ApiResult implements Serializable {

    private static final long serialVersionUID = -8440958610795020343L;

    public static final int CODE_SUCCESS = 200;
    public static final int CODE_ERROR = 500;
    public static final int CODE_WARNING = 501;
    public static final int CODE_NOT_PERMISSION = 403;
    public static final int CODE_NOE_LOGIN = 401;
    public static final int CODE_INVALID_REQUEST = 400;

    private int code;
    private String msg;
    private Object data;

    // 构造方法
    public ApiResult(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    // 静态构造方法
    public static ApiResult success() {
        return new ApiResult(CODE_SUCCESS, null, null);
    }

    public static ApiResult success(String msg) {
        return new ApiResult(CODE_SUCCESS, msg, null);
    }

    public static ApiResult success(String msg, Object data) {
        return new ApiResult(CODE_SUCCESS, msg, data);
    }

    public static ApiResult success(Object data) {
        return new ApiResult(CODE_SUCCESS, null, data);
    }

    public static ApiResult error() {
        return new ApiResult(CODE_ERROR, "出错了", null);
    }

    public static ApiResult error(String msg) {
        return new ApiResult(CODE_ERROR, msg, null);
    }

    public static ApiResult error(String msg, Object data) {
        return new ApiResult(CODE_ERROR, msg, data);
    }

    public static ApiResult notLogin() {
        return new ApiResult(CODE_NOE_LOGIN, "未登录，请先进行登录", null);
    }

    public static ApiResult noPermissions() {
        return new ApiResult(CODE_NOT_PERMISSION, "权限不足", null);
    }

    public static ApiResult build(int code, String msg) {
        return new ApiResult(code, msg, null);
    }

    public static ApiResult getByBoolean(boolean b) {
        return b ? success() : error();
    }

    public static ApiResult warning(String msg, Object data) {
        return new ApiResult(CODE_WARNING, msg, data);
    }

    public static ApiResult warning(String msg) {
        return new ApiResult(CODE_WARNING, msg, null);
    }

    public <T> Optional<T> getData(Class<T> clazz) {
        return Optional.ofNullable(clazz.cast(data));
    }

    @Override
    public String toString() {
        return "ApiResult{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + (data != null ? data.toString() : "null") +
                '}';
    }
}